﻿Public Class Common
    Public Shared Function IncludeTrailingPathDelimiter(ByVal APath As String) As String
        If APath.Substring(APath.Length - 1) = System.IO.Path.DirectorySeparatorChar Then
            Return APath
        Else
            Return APath & System.IO.Path.DirectorySeparatorChar
        End If
    End Function
    Public Shared Function Left(ByVal AValue As String, ByVal ALength As Integer) As String
        If ALength <= AValue.Length Then
            Return AValue.Substring(0, ALength)
        End If
        Return AValue
    End Function
    Public Shared Function Right(ByVal AValue As String, ByVal ALength As Integer) As String
        If ALength <= AValue.Length Then
            Return AValue.Substring(AValue.Length - ALength)
        End If
        Return AValue
    End Function
    Public Shared Function Mid(ByVal AValue As String, ByVal AStart As Integer, ByVal ALength As Integer) As String
        If AStart <= AValue.Length Then
            If AStart + ALength - 1 <= AValue.Length Then
                Return AValue.Substring(AStart - 1, ALength)
            End If
            Return AValue.Substring(AStart - 1)
        End If
        Return String.Empty
    End Function

    Public Shared Function StrToIntDef(ByVal AValue As String, ByVal ADefault As Integer) As Integer
        Dim ret As Integer
        If Integer.TryParse(AValue, ret) Then
            Return ret
        Else
            Return ADefault
        End If
    End Function
    Public Shared Function DateTimeToString(ByVal ADateTime As DateTime) As String
        If ADateTime.Equals(DateTime.MinValue) Then
            Return String.Empty
        Else
            Return ADateTime.ToString("yyyy/MM/dd HH:mm:ss")
        End If
    End Function
    Public Shared Function ReversedColor(ByVal AColor As Color) As Color
        Return Color.FromArgb(Math.Abs(AColor.R - &HFF), Math.Abs(AColor.G - &HFF), Math.Abs(AColor.B - &HFF))
    End Function

    Public Shared Sub SetGridAllowSorting(ByRef AGrid As DataGridView, ByVal AValue As DataGridViewColumnSortMode)
        For Each col As DataGridViewColumn In AGrid.Columns
            col.SortMode = AValue
        Next
    End Sub
    Public Shared Function ApplicationPath() As String
        Return IncludeTrailingPathDelimiter(System.AppDomain.CurrentDomain.BaseDirectory)
    End Function

End Class
